import { END_PRESS } from "../../../components/LongPress";
import EventManager from "../../../core/EventManager";
import ResManager from "../../../core/ResManager";
import { ResType } from "../../../net/Commond";
import { BattleTest } from "../../data/BattleTest";
import { ResPath, SkillIconType } from "../../data/CFCommon";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemSkillIcon extends cc.Component {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    private skillId: number = 0;
    private idx: number = 0;
    private type: SkillIconType = SkillIconType.None;
    public sortIdx: number = 0;

    protected start(): void {
        this.node.on(END_PRESS, this.OnTouchEnd, this);
    }

    init(skillId, idx, type) {
        this.skillId = skillId;
        this.idx = idx;
        this.type = type;
        let config_skill = ResManager.ins().getRes('config_skill');
        let skillInfo = config_skill[skillId];
        if (skillInfo) {
            ResManager.ins().loadSingle(skillInfo.skillIcon, ResType.image, ResPath.skill_icon, (asset) => {
                this.icon.spriteFrame = asset;
            }, this.node);
        }
    }

    clear() {
        this.skillId = 0;
        this.idx = 0;
        this.type = 0;
        this.icon.spriteFrame = null;
    }

    getSkillId() {
        return this.skillId;
    }

    OnTouchEnd() {
        // console.log("skillid==", this.skillId, ",idx==", this.idx, ",type==", this.type);
        let callback = () => {
            if (!BattleTest.ins().isFighting && this.type == SkillIconType.FightSkillIcon && this.skillId > 0) {
                this.clear();
            }
        }
        EventManager.emit("onSkillIconClick", this.skillId, this.idx, this.type, callback);
    }

    OnLonbgTouch() {
        EventManager.emit("ItemSkillIconLongTouch", this.skillId);
    }
}
